import {BasicColumn} from '/@/components/Table';
import {FormSchema} from '/@/components/Table';
import { rules} from '/@/utils/helper/validator';
import { render } from '/@/utils/common/renderUtils';
import { getWeekMonthQuarterYear } from '/@/utils';
import { h } from 'vue';
import dayjs from 'dayjs';
import { defHttp } from '/@/utils/http/axios';
import { message, Tag } from 'ant-design-vue';

// 创建一个服务任务调用状态的映射表
const serviceTaskCalledMap: Record<string, boolean> = {};

// 状态判断函数
function getTaskStatus(record) {
  const now = dayjs();
  const startTime = record.startTime ? dayjs(record.startTime) : null;
  const endTime = record.endTime ? dayjs(record.endTime) : null;
  
  // 如果已经有状态值，则使用已有状态
  if (record.status) {
    return record.status;
  }
  
  // 自动判断状态
  if (!startTime || !endTime) {
    return '待开始';
  } else if (now.isBefore(startTime)) {
    return '待开始';
  } else if (now.isAfter(endTime)) {
    return '已延期';
  } else if (now.isAfter(startTime) && now.isBefore(endTime)) {
    // 自动变更为进行中时，调用接口
    // 检查是否已经调用过该记录的接口
    if (!record._serviceTaskCalled && record.id && !serviceTaskCalledMap[record.id]) {
      record._serviceTaskCalled = true;
      
      if (record.id) {
        serviceTaskCalledMap[record.id] = true;
        
        // 用setTimeout延迟执行，避免频繁调用
        setTimeout(() => {
          defHttp.post({ url: '/tasks/tasks/addServiceTask', params: record }).then(() => {
            console.log('服务任务已自动提交');
          }).catch(() => {
            console.error('服务任务提交失败');
            // 重置状态，允许下次重试
            if (record.id) {
              serviceTaskCalledMap[record.id] = false;
            }
          });
        }, 500);
      }
    }
    return '进行中';
  } else if (record.status === '已完成') {
    return '已完成';
  } else {
    return '进行中';
  }
}

// 获取状态对应的颜色
function getStatusColor(status) {
  switch (status) {
    case '待开始':
      return 'orange';
    case '进行中':
      return 'blue';
    case '已完成':
      return 'green';
    case '已延期':
      return 'red';
    default:
      return '';
  }
}

//列表数据
export const columns: BasicColumn[] = [
  {
    title: '序号',
    align: 'center',
    dataIndex: 'index',
    width: 60,
    customRender: ({ index }) => index + 1,
  },
  {
    title: '单位名称',
    align: "center",
    dataIndex: 'itemName',
    customRender: ({ text, record }) => {
      // 处理数据库字段名映射
      return record.itemName || record.item_name || text || '';
    }
  },

  {
    title: '团队名',
    align: "center",
    dataIndex: 'teamName',
    customRender: ({ text, record }) => {
      // 处理数据库字段名映射
      return record.teamName || record.team_name || text || '';
    }
  },
  {
    title: '产品类型',
    align: "center",
    dataIndex: 'productType',
    customRender: ({ text, record }) => {
      // 处理数据库字段名映射
      return record.productType || record.product_type || text || '';
    }
  },
  {
    title: '产品名称',
    align: "center",
    dataIndex: 'productName',
    customRender: ({ text, record }) => {
      // 处理数据库字段名映射
      return record.productName || record.product_name || text || '';
    }
  },
  {
    title: '任务状态',
    align: "center",
    dataIndex: 'taskStatus',
    customRender: ({ text, record }) => {
      // 处理数据库字段名映射和状态显示
      const status = record.status;
      const color = status === '已完成' ? 'green' : 'orange';
      return h(Tag, { color }, () => status);
    }
  },
  {
    title: '开始时间',
    align: "center",
    dataIndex: 'startTime',
    customRender: ({ text, record }) => {
      // 处理数据库字段名映射
      const time = record.startTime || record.start_time || text;
      return time ? (time.length > 10 ? time.substr(0,10) : time) : '';
    },
  },
  {
    title: '结束时间',
    align: "center",
    dataIndex: 'endTime',
    customRender: ({ text, record }) => {
      // 处理数据库字段名映射
      const time = record.endTime || record.end_time || text;
      return time ? (time.length > 10 ? time.substr(0,10) : time) : '';
    },
  },
  {
    title: '创建时间',
    align: "center",
    dataIndex: 'createTime',
    customRender: ({ text, record }) => {
      // 处理数据库字段名映射
      const time = record.createTime || record.create_time || text;
      return time ? (time.length > 10 ? time.substr(0,10) : time) : '';
    },
  },
];



// 子表列配置 - 团队成员任务分配
export const teamMemberColumns = [
  {
    title: '成员姓名',
    dataIndex: 'name',
    key: 'name',
    width: 120,
  },
  {
    title: '角色',
    dataIndex: 'role',
    key: 'role',
    width: 80,
  },
  {
    title: '产品名称',
    dataIndex: 'productName',
    key: 'productName',
    width: 150,
    customRender: ({ text }) => text || '暂无产品',
  },
  {
    title: '任务内容',
    dataIndex: 'assignmentContent',
    key: 'assignmentContent',
    width: 200,
    customRender: ({ text }) => text || '暂无任务内容',
  },
  {
    title: '完成程度',
    dataIndex: 'assignmentStatus',
    key: 'assignmentStatus',
    width: 120,
    slots: { customRender: 'assignmentStatus' },
  },
  {
    title: '操作',
    dataIndex: 'action',
    key: 'action',
    width: 100,
    fixed: 'right',
    slots: { customRender: 'action' },
  },
];

// 添加展开行配置
export const tableProps = {
  canExpand: true,
};

// 高级查询数据
export const superQuerySchema = {
  contractsDetailInfoId: {title: '子合同ID',order: 0,view: 'text', type: 'string',},
  itemName: {title: '单位名称',order: 1,view: 'text', type: 'string',},
  teamName: {title: '团队名',order: 2,view: 'text', type: 'string',},
  productType: {title: '产品类型',order: 3,view: 'text', type: 'string',},
  productName: {title: '产品名称',order: 4,view: 'text', type: 'string',},
  taskStatus: {title: '任务状态',order: 5,view: 'text', type: 'string',},
  startTime: {title: '开始时间',order: 6,view: 'date', type: 'string',},
  endTime: {title: '结束时间',order: 7,view: 'date', type: 'string',},
  createTime: {title: '创建时间',order: 8,view: 'datetime', type: 'string',},
};
